// 用于生成随机id（app端需要用到）
import { uuid } from 'vue-uuid'
import BigNumber from 'bignumber.js'
import apiUrl from '../js/apiUrl'

import { StructureSocket } from './basicsSocket'

// 聊天消息的socket地址必需是动态获取的 (此处设置无效)
// 进入home页面，重新设置socket地址
let wssUri = apiUrl.webws
export const openChatSocket = new StructureSocket(wssUri)

/**
 * 发送数据
 * @param data 格式为json
 */
export function sendData (data) {
  if (openChatSocket.ws.readyState === 1) {
    openChatSocket.ws.send(JSON.stringify(data))
  }
}

/**
 * 发送普通单聊文本消息
 * @param msg 文本信息
 * @param roomId 房间id
 * @param receiverId 接收方id
 */
export function sendSingleChatText (msg, roomId, receiverId, requestId = uuid.v1()) {
  const data = {
    msgType: roomId === '100008' ? 'fileHelper' : 'singleChat',
    subType: 'text',
    requestId,
    data: {
      isEncrypted: 0,
      receiverId: receiverId,
      backId: requestId,
      msg: msg,
      roomId: roomId,
      timestamp: BigNumber(new Date().getTime()).toString()
    }
  }
  sendData(data)
  return data
}

/**
 * 发送普通单聊图片消息
 * @param msg 图片的url
 * @param roomId 房间id
 * @param receiverId 接收方id
 */
export function sendSingleChatImage (msg, roomId, receiverId, img, requestId = uuid.v1(), sendId, recediveId) {
  const {
    fileName,
    fileSize,
    width,
    height
  } = img
  const data = {
    msgType: roomId === '100008' ? 'fileHelper' : 'singleChat',
    subType: 'image',
    requestId,
    data: {
      isEncrypted: 0,
      receiverId: receiverId,
      sourceId: msg,
      fileName,
      backId: requestId,
      fileSize,
      width,
      height,
      roomId: roomId,
      timestamp: BigNumber(new Date().getTime()).toString(),
      sendId,
      recediveId
    }
  }
  sendData(data)
  return data
}
/**
 * 发送群聊撤回消息
 * @param timestamp 消息发送的时间
 * @param roomId 房间ID
 * @param requestId 回执ID
 */
export function sendRecallGroup (timestamp, roomId, requestId, messageId) {
  const temp = {
    msgType: 'groupChat',
    subType: 'recall',
    requestId,
    data: {
      timestamp,
      messageId,
      roomId,
      isEncrypted: 0
    }
  }
  sendData(temp)
  return temp
}

/**
 * 发送群聊撤回消息
 * @param timestamp 消息发送的时间
 * @param roomId 房间ID
 * @param requestId 回执ID
 */
export function sendRecallSingle (timestamp, roomId, requestId, messageId, receiverId) {
  const temp = {
    msgType: 'singleChat',
    subType: 'recall',
    requestId,
    data: {
      timestamp,
      roomId,
      messageId,
      isEncrypted: 0,
      receiverId
    }
  }
  sendData(temp)
  return temp
}

/**
 * 发送普通群聊图片消息
 * @param msg 图片的url
 * @param roomId 房间id
 * @param receiverId 接收方id
 */
export function sendGroupleChatImage (msg, roomId, img, requestId = uuid.v1(), sendId, recediveId) {
  const {
    fileName,
    fileSize,
    width,
    height
  } = img
  const data = {
    msgType: 'groupChat',
    subType: 'image',
    requestId,
    data: {
      isEncrypted: 0,
      sourceId: msg,
      fileName,
      fileSize,
      backId: requestId,
      width,
      height,
      roomId: roomId,
      timestamp: BigNumber(new Date().getTime()).toString(),
      sendId,
      recediveId
    }
  }
  sendData(data)
  return data
}

/**
 * 发送普通群聊视频消息
 * @param msg 视频的url
 * @param roomId 房间id
 * @param receiverId 接收方id
 * @param duration 视频秒数
 * @param requestId
 */
export function sendGroupChatVideo (msg, roomId, video, duration, requestId = uuid.v1(), sendId, recediveId) {
  const {
    fileName,
    fileSize,
    width,
    height,
    frameUrl
  } = video
  const data = {
    msgType: 'groupChat',
    subType: 'video',
    requestId,
    data: {
      isEncrypted: 0,
      sourceId: msg,
      fileName,
      fileSize,
      width,
      height,
      backId: requestId,
      roomId: roomId,
      frameUrl,
      duration,
      timestamp: BigNumber(new Date().getTime()).toString(),
      sendId,
      recediveId
    }
  }
  sendData(data)
  return data
}

/**
 * 发送普通单聊视频消息
 * @param msg 视频的url
 * @param roomId 房间id
 * @param receiverId 接收方id
 * @param duration 视频秒数
 */
export function sendSingleChatVideo (msg, roomId, receiverId, video, duration, requestId = uuid.v1(), sendId, recediveId) {
  const {
    fileName,
    fileSize,
    width,
    height,
    frameUrl
  } = video
  const data = {
    msgType: roomId === '100008' ? 'fileHelper' : 'singleChat',
    subType: 'video',
    requestId,
    data: {
      isEncrypted: 0,
      receiverId: receiverId,
      sourceId: msg,
      fileName,
      fileSize,
      width,
      height,
      roomId: roomId,
      backId: requestId,
      frameUrl,
      duration,
      timestamp: BigNumber(new Date().getTime()).toString(),
      sendId,
      recediveId
    }
  }
  sendData(data)
  return data
}

/**
 * 发送普通群聊文件消息
 * @param msg 文件的url
 * @param roomId 房间id
 * @param receiverId 接收方id
 */
export function sendGroupChatFile (msg, roomId, file, requestId = uuid.v1(), sendId, recediveId) {
  const {
    fileName,
    fileSize
  } = file
  const data = {
    msgType: 'groupChat',
    subType: 'file',
    requestId,
    data: {
      isEncrypted: 0,
      sourceId: msg,
      roomId: roomId,
      fileName,
      backId: requestId,
      fileSize,
      timestamp: BigNumber(new Date().getTime()).toString(),
      sendId,
      recediveId
    }
  }
  sendData(data)
  return data
}

/**
 * 发送普通单聊文件消息+
 * @param msg 文件的url
 * @param roomId 房间id
 * @param receiverId 接收方id
 */
export function sendSingleChatFile (msg, roomId, receiverId, file, requestId = uuid.v1(), sendId, recediveId) {
  const {
    fileName,
    fileSize
  } = file
  const data = {
    msgType: roomId === '100008' ? 'fileHelper' : 'singleChat',
    subType: 'file',
    requestId,
    data: {
      isEncrypted: 0,
      receiverId: receiverId,
      sourceId: msg,
      roomId: roomId,
      backId: requestId,
      fileName,
      fileSize,
      timestamp: BigNumber(new Date().getTime()).toString(),
      sendId,
      recediveId
    }
  }
  sendData(data)
  return data
}

/**
 * 发送普通群聊文本消息
 * @param 文本信息
 * @param roomId 房间id
 */
export function sendGroupChatText (msg, roomId, requestId = uuid.v1()) {
  const data = {
    msgType: 'groupChat',
    subType: 'text',
    requestId,
    data: {
      isEncrypted: 0,
      msg: msg,
      roomId: roomId,
      backId: requestId,
      timestamp: BigNumber(new Date().getTime()).toString()
    }
  }
  // if (atModelList.length > 0) data.data.atModelList = atModelList

  sendData(data)
  return data
}
/**
 * 艾特普通群聊成员消息
 * @param 文本信息
 * @param roomId 房间id
 */
export function sendGroupChatAt (msg, atModelList, roomId, requestId = uuid.v1()) {
  const data = {
    msgType: 'groupChat',
    subType: 'at',
    requestId,
    data: {
      isEncrypted: 0,
      msg: msg,
      roomId: roomId,
      backId: requestId,
      timestamp: Date.parse(new Date()),
      atModelList
    }
  }
  // console.log('dataaaaaaaaaaaa', data)
  sendData(data)
  return data
}
